package com.weixin.paleituo.adapter;

import android.content.Context;
import android.text.TextUtils;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.bumptech.glide.Glide;
import com.weixin.paleituo.R;
import com.weixin.paleituo.base.BaseRecycleAdapter;
import com.weixin.paleituo.bean.GoodsSpecifBean;
import com.weixin.paleituo.bean.OrderGoods;
import com.weixin.paleituo.help.RecycleViewHolder;
import com.weixin.paleituo.util.StringUtils;
import com.weixin.paleituo.util.UIUtils;

import java.text.DecimalFormat;
import java.util.ArrayList;

import butterknife.BindView;
import butterknife.ButterKnife;

/**
 * 商品清单列表查看
 * 这里只做展示效果，没有任何页面交互
 *
 * @author fengzhen
 * @version v1.0, 2018/4/1
 */
public class GoodsListPreRecycleAdapter extends BaseRecycleAdapter<OrderGoods> {

    private DecimalFormat format = new DecimalFormat("#0.00");

    public GoodsListPreRecycleAdapter(Context context, ArrayList<OrderGoods> datas) {
        super(context, datas, R.layout.item_goods_list_pre);
    }

    @Override
    protected void setData(RecycleViewHolder holder, OrderGoods s, int position) {
        ViewHolder viewHolder = (ViewHolder) holder;
        OrderGoods model = mDatas.get(position);
        Glide.with(mContext)
                .load(model.getListUrl())
                .into(viewHolder.mImgGoodsItemGoodsListPre);
        viewHolder.mTvNameItemGoodsListPre.setText(model.getGoodsName());
        ArrayList<GoodsSpecifBean> specification = model.getGoodsSpecsList();
        StringBuffer spe = new StringBuffer();
        if (specification != null) {
            for (GoodsSpecifBean bean :
                    specification) {
                spe.append(bean.getTypeName())
                        .append("：")
                        .append(bean.getName())
                        .append(" ");
            }
        } else {
            spe.append(model.getGoodsSpecsInfo());
        }
        viewHolder.mTvInfoItemGoodsListPre.setText(spe);
        viewHolder.mTvCountEachItemGoodsListPre.setText(String.format(mContext.getString(R.string.string_x), model.getCount()));
        if (StringUtils.isEmpty(model.getPrice())) {
            return;
        }
        if (model.isVip()) {
            if (StringUtils.isEmpty(model.getSpecialSales())) {
                return;
            }
            viewHolder.mTvPriceItemGoodsListPre.setVisibility(View.GONE);
            viewHolder.mLinearVipItemGoodsListPre.setVisibility(View.VISIBLE);
            viewHolder.mTvOriginalPriceItemGoodsListPre.setText(String.format(mContext.getString(R.string.string_pay), format.format(Double.valueOf(model.getPrice()))));
            viewHolder.mTvCurrentPriceItemGoodsListPre.setText(String.format(mContext.getString(R.string.string_pay), format.format(Double.valueOf(model.getSpecialSales()))));
        } else {
            viewHolder.mTvPriceItemGoodsListPre.setVisibility(View.VISIBLE);
            viewHolder.mLinearVipItemGoodsListPre.setVisibility(View.GONE);
            viewHolder.mTvPriceItemGoodsListPre.setText(String.format(mContext.getString(R.string.string_pay), format.format(Double.valueOf(model.getPrice()))));
        }
    }

    @Override
    public RecycleViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
        return new ViewHolder(UIUtils.inflate(mContext, R.layout.item_goods_list_pre, parent, false));
    }

    static class ViewHolder extends RecycleViewHolder {
        @BindView(R.id.img_goods_item_goods_list_pre)
        ImageView mImgGoodsItemGoodsListPre;
        @BindView(R.id.tv_name_item_goods_list_pre)
        TextView mTvNameItemGoodsListPre;
        @BindView(R.id.tv_info_item_goods_list_pre)
        TextView mTvInfoItemGoodsListPre;
        @BindView(R.id.tv_price_item_goods_list_pre)
        TextView mTvPriceItemGoodsListPre;
        @BindView(R.id.tv_current_price_item_goods_list_pre)
        TextView mTvCurrentPriceItemGoodsListPre;
        @BindView(R.id.tv_original_price_item_goods_list_pre)
        TextView mTvOriginalPriceItemGoodsListPre;
        @BindView(R.id.linear_vip_item_goods_list_pre)
        LinearLayout mLinearVipItemGoodsListPre;
        @BindView(R.id.tv_count_each_item_goods_list_pre)
        TextView mTvCountEachItemGoodsListPre;

        ViewHolder(View view) {
            super(view);
            ButterKnife.bind(this, view);
        }
    }
}
